//-----------------------------------------------------------------------
// <copyright file="ResponsibleOfficeDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ResponsibleOfficeDAO : ResponsibleOfficeDAOBase
	{
		public ResponsibleOfficeDAO()
		{
		}

		public virtual void DeleteResponsibleOfficeByConPoint(int conPointID)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesDeleteByConPoint");
                
				dbCommandWrapper.AddInParameter("@conPointID", DbType.Int32, conPointID);				
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessDeleteResponsibleOfficeException, ex);
			}
		}

		public virtual ResponsibleOfficeCollection GetResponsibleOfficesByResponsible(int responsibleID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesGetListByResponsible");
                
				dbCommandWrapper.AddInParameter("@responsibleID", DbType.Int32, responsibleID);
   
				ResponsibleOfficeCollection responsibleOfficeCollection = new ResponsibleOfficeCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						ResponsibleOffice responsibleOffice = CreateResponsibleOfficeFromReader(reader);					

						responsibleOfficeCollection.Add(responsibleOffice);
					}
					reader.Close();
				}
				
				return responsibleOfficeCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetResponsibleOfficesException, ex);
			}
		}

		public virtual ResponsibleOfficeCollection GetResponsibleOfficesByConPoint(int conPointID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesGetListByConpoint");
                
				dbCommandWrapper.AddInParameter("@conPointID", DbType.Int32, conPointID);
   
				ResponsibleOfficeCollection responsibleOfficeCollection = new ResponsibleOfficeCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						ResponsibleOffice responsibleOffice = CreateResponsibleOfficeFromReader(reader);

						if (!reader.IsDBNull(reader.GetOrdinal("IsInUse"))) responsibleOffice.IsInUse = Convert.ToBoolean(reader["IsInUse"]);

						responsibleOfficeCollection.Add(responsibleOffice);
					}
					reader.Close();
				}
				
				return responsibleOfficeCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetResponsibleOfficesException, ex);
			}
		}
	}
}
